const getQueryParams = (url: string): Record<string, string> => {
    const result: Record<string, string> = {};
    const queryString = url.split('?')[1];
    if (!queryString) return result;

    const params = new URLSearchParams(queryString);
    params.forEach((value, key) => {
        result[key] = value;
    });

    return result;
};

const getPathParams = (pattern: string, path: string): Record<string, string> => {
    const paramNames: string[] = [];
    const regexPattern = pattern.replace(/:([^/]+)/g, (_, key) => {
        paramNames.push(key);
        return '([^/]+)';
    });

    const regex = new RegExp(`^${regexPattern}$`);
    const match = path.match(regex);

    if (!match) return {};

    return paramNames.reduce((acc, key, i) => {
        acc[key] = match[i + 1];
        return acc;
    }, {} as Record<string, string>);
};

// 示例
// console.log(getPathParams("/user/:id/book/:bookId", "/user/123/book/456"));
// { id: "123", bookId: "456" }

export { getQueryParams, getPathParams };  

